//
//  MineViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class MineViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    private let kMineTableViewCell = "MineTableViewCell"
    
    var isLogin = false
    
    lazy var viewModel: MineViewModel = {
        let vm = MineViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_userinfo  {
                if isSuccess {
                    self?.reloadHeaderView()
                }
            }
        }
        return vm
    }()
    
    lazy var tableViewHeaderView: MineHeaderView = {
        let view = MineHeaderView.loadFromNib() as! MineHeaderView
//        view.isLogin = isLogin
        view.addTouchTarget(self, action: #selector(gotoUserInfo))
        return view
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.themeBackground = ThemeManager.ThemeColorKeys.margin
        table.register(UINib(nibName: kMineTableViewCell, bundle: nil), forCellReuseIdentifier: kMineTableViewCell)
        table.tableFooterView = UIView()
        table.separatorStyle = .none
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    func initUI() {
        self.title = ""
        tableViewHeaderView.height = 130
        tableView.tableHeaderView = tableViewHeaderView
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        btn.themeSetImage(ThemeManager.ThemeImageKeys.mineSetting, for: .normal)
        btn.addTarget(self, action: #selector(gotoSetting), for: .touchUpInside)
        let item = UIBarButtonItem(customView: btn)
        self.navigationItem.rightBarButtonItem = item
        view.addSubview(tableView)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableViewHeaderView.isLogin = AppInfo.default.isLogin
        if AppInfo.default.isLogin {
        self.viewModel.refreshUserinfo()
        }
    }
    
    // MARK: - UITableViewDelegate UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = viewModel.dataArray[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: kMineTableViewCell, for: indexPath) as! MineTableViewCell
        cell.setData(data: data)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.row == 0 {
            if !AppInfo.default.isLogin {
                self.navigationController?.pushViewController(LoginViewController(), animated: true)
                return
            }
            self.navigationController?.pushViewController(AssetViewController(), animated: true)
        }else if indexPath.row == 1 {
            if !AppInfo.default.isLogin {
                self.navigationController?.pushViewController(LoginViewController(), animated: true)
                return
            }
            self.navigationController?.pushViewController(CoinAddressViewController(), animated: true)
        }else if indexPath.row == 2 {
            if !AppInfo.default.isLogin {
                self.navigationController?.pushViewController(LoginViewController(), animated: true)
                return
            }
            self.navigationController?.pushViewController(SecurityCenterViewController(), animated: true)
        }else if indexPath.row == 3 {
            let vc = HelpOrFeedbackViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else if indexPath.row == 4 {
            let vc = RecommendViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func reloadHeaderView() {
        self.tableViewHeaderView.reload()
    }
    
    @objc func gotoSetting() {
        self.navigationController?.pushViewController(SettingViewController(), animated: true)
    }
    
    @objc func gotoUserInfo() {
        var vc: UIViewController
        if AppInfo.default.isLogin {
            vc = UserInfoViewController()
        }else {
            vc = LoginViewController()
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
